package com.example.careycai.playactivity;


import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;


import java.util.List;

import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;



public class Homescreen extends Fragment {
    private Button btn;
    // 声明控件
    private ViewPager mViewPager;
    private List<ImageView> mlist;
    private TextView mTextView;
    private LinearLayout mLinearLayout;
    private TextView wangluo_more;
    private TextView shouji_more;
    // 广告图素材
    private int[] bannerImages = { R.drawable.banner1, R.drawable.banner2, R.drawable.banner3, R.drawable.banner4 };
    // 广告语
    private String[] bannerTexts = { "因为专业 所以卓越", "坚持创新 行业领跑", "诚信 专业 双赢", "精细 和谐 大气 开放" };

    // ViewPager适配器与监听器
    private BannerAdapter mAdapter;
    private BannerListener bannerListener;

    // 圆圈标志位
    private int pointIndex = 0;
    // 线程标志
    private boolean isStop = false;




    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);


    }
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.activity_homescreen, container, false);
        wangluo_more=(TextView)view.findViewById(R.id.wangluo_more);
        shouji_more=(TextView)view.findViewById(R.id.shouji_more);

        wangluo_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String more="网络游戏";
                Intent intent=new Intent(getActivity(),more.class);
                intent.putExtra("more",more);
                startActivity(intent);
            }
        });
        shouji_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String more="手机游戏";
                Intent intent=new Intent(getActivity(),more.class);
                intent.putExtra("more", more);
                startActivity(intent);
            }
        });
        mViewPager = (ViewPager)view.findViewById(R.id.viewpager);
        mTextView = (TextView)view.findViewById(R.id.tv_bannertext);
        mLinearLayout = (LinearLayout)view.findViewById(R.id.points);

        mlist = new ArrayList<ImageView>();
        View view1;
        LayoutParams params;
        for (int i = 0; i < bannerImages.length; i++) {
            // 设置广告图
            ImageView imageView = new ImageView(getActivity());
            imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            imageView.setBackgroundResource(bannerImages[i]);
            mlist.add(imageView);
            // 设置圆圈点
            view1 = new View(getActivity());
            params = new LayoutParams(5, 5);
            params.leftMargin = 10;
            view1.setBackgroundResource(R.drawable.point_background);
            view1.setLayoutParams(params);
            view1.setEnabled(false);

            mLinearLayout.addView(view1);
        }
        mAdapter = new BannerAdapter(mlist);
        mViewPager.setAdapter(mAdapter);

        bannerListener = new BannerListener();
        mViewPager.setOnPageChangeListener(bannerListener);
        //取中间数来作为起始位置
        int index = (Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2 % mlist.size());
        //用来出发监听器
        mViewPager.setCurrentItem(index);
        mLinearLayout.getChildAt(pointIndex).setEnabled(true);

        // 开启新线程，2秒一次更新Banner
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (!isStop) {
                    SystemClock.sleep(2000);
                    try {
                        getActivity().runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
                            }
                        });

                    }catch (Exception E){

                    }

                }
            }

        }).start();




        GridView gridview = (GridView)view.findViewById(R.id.GridView);
        GridView gridview2 = (GridView)view.findViewById(R.id.GridView2);
        GridView gridview3 = (GridView)view.findViewById(R.id.GridView3);

        //最新
        final ArrayList<HashMap<String, Object>> meumList1 = new ArrayList<HashMap<String, Object>>();
        //网络游戏
        ArrayList<HashMap<String, Object>> meumList2 = new ArrayList<HashMap<String, Object>>();
        //手机游戏
        ArrayList<HashMap<String, Object>> meumList3 = new ArrayList<HashMap<String, Object>>();
        //最新
        for (int i = 1; i < 3; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            if (i == 1) {
                map.put("ItemImage", R.drawable.zuixin1);
                map.put("ItemText", "穿越火线CF");
            }
            if (i == 2) {
                map.put("ItemImage", R.drawable.zuixin2);
                map.put("ItemText", "穿越火线CF" + i);
            }
            meumList1.add(map);
        }
        //网络游戏
        for (int i = 1; i < 5; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();

            if (i == 1) {
                map.put("ItemImage", R.drawable.wangluo1);
                map.put("ItemText", "穿越火线CF" + i);
            }

            if (i == 2) {
                map.put("ItemImage", R.drawable.wangluo2);
                map.put("ItemText", "穿越火线CF" + i);
            }

            if (i == 3) {
                map.put("ItemImage", R.drawable.wangluo3);
                map.put("ItemText", "穿越火线CF" + i);
            }

            if (i == 4) {
                map.put("ItemImage", R.drawable.wangluo4);
                map.put("ItemText", "穿越火线CF" + i);
            }
            meumList2.add(map);
        }
        //手机游戏
        for (int i = 1; i < 5; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();

            if (i == 1) {
                map.put("ItemImage", R.drawable.shouji1);
                map.put("ItemText", "保卫萝卜");
            }

            if (i == 2) {
                map.put("ItemImage", R.drawable.shouji2);
                map.put("ItemText","天天酷跑");
            }

            if (i == 3) {
                map.put("ItemImage", R.drawable.shouji3);
                map.put("ItemText", "切水果");
            }

            if (i == 4) {
                map.put("ItemImage", R.drawable.shouji4);
                map.put("ItemText", "植物大战僵尸");
            }
            meumList3.add(map);
        }
        //最新
        SimpleAdapter saMenuItem1 = new SimpleAdapter(getActivity(),
                meumList1, //数据源
                R.layout.homeitem, //xml实现
                new String[]{"ItemImage", "ItemText"}, //对应map的Key
                new int[]{R.id.ItemImage, R.id.ItemText});  //对应R的Id

        //网络游戏
        SimpleAdapter saMenuItem2 = new SimpleAdapter(getActivity(),
                meumList2, //数据源
                R.layout.homeitem, //xml实现
                new String[]{"ItemImage", "ItemText"}, //对应map的Key
                new int[]{R.id.ItemImage, R.id.ItemText});  //对应R的Id

        //手机游戏
        SimpleAdapter saMenuItem3 = new SimpleAdapter(getActivity(),
                meumList3, //数据源
                R.layout.homeitem, //xml实现
                new String[]{"ItemImage", "ItemText","qi"}, //对应map的Key
                new int[]{R.id.ItemImage, R.id.ItemText,R.id.qi});  //对应R的Id

        //添加Item到最新网格中
        gridview.setAdapter(saMenuItem1);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                                                System.out.println("click index:" + arg2);
                                                int index = arg2 + 1;//id是从0开始的，所以需要+1
                                                String data="逆战";
                                                Intent intent=new Intent(getActivity(),games_item.class);
                                                intent.putExtra("extra_data",data);
                                                startActivity(intent);
                                            }
                                        }
        );

        //添加Item到网络游戏网格中
        gridview2.setAdapter(saMenuItem2);
        gridview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                                                 System.out.println("click index:" + arg2);
                                                 int index = arg2 + 1;//id是从0开始的，所以需要+1
                                                 String data="对应网络游戏名称";
                                                 Intent intent=new Intent(getActivity(),games_item.class);
                                                 intent.putExtra("extra_data",data);
                                                 startActivity(intent);
                                             }
                                         }
        );

        //添加Item到手机游戏网格中
        gridview3.setAdapter(saMenuItem3);
        gridview3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                                                 System.out.println("click index:" + arg2);
                                                 int index = arg2 + 1;//id是从0开始的，所以需要+1
                                                 String data="对应手机游戏名称";
                                                 Intent intent=new Intent(getActivity(),games_item.class);
                                                 intent.putExtra("extra_data",data);
                                                 startActivity(intent);
                                             }
                                         }
        );

        return  view;
    }

    //实现VierPager监听器接口
    class BannerListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
            int newPosition = position % bannerImages.length;
            mTextView.setText(bannerTexts[newPosition]);
            mLinearLayout.getChildAt(newPosition).setEnabled(true);
            mLinearLayout.getChildAt(pointIndex).setEnabled(false);
            // 更新标志位
            pointIndex = newPosition;

        }

    }

    @Override
    public void onDestroy() {
        // 关闭定时器
        isStop = true;
        super.onDestroy();
    }


}
